package io.cici.cc.mybatis.plus.core;

import io.cici.cc.mybatis.lite.exceptions.ExceptionFactory;
import io.cici.cc.mybatis.lite.session.Configuration;
import io.cici.cc.mybatis.lite.session.SqlSessionFactory;
import io.cici.cc.mybatis.lite.session.SqlSessionFactoryBuilder;
import io.cici.cc.mybatis.plus.core.config.GlobalConfig;
import io.cici.cc.mybatis.plus.core.incrementer.DefaultIdGenerator;
import io.cici.cc.mybatis.plus.core.incrementer.IdGenerator;
import io.cici.cc.mybatis.plus.core.injector.SqlRunnerInjector;
import io.cici.cc.mybatis.plus.core.toolkit.GlobalConfigUtils;
import io.cici.cc.mybatis.plus.core.toolkit.IdWorker;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;


public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {

    @SuppressWarnings("Duplicates")
    @Override
    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        try {
            // TODO 这里换成 MybatisXMLConfigBuilder 而不是 XMLConfigBuilder
            MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(reader, environment, properties);
            return build(parser.parse());
        } catch (Exception e) {
            throw ExceptionFactory.wrapException(e);
        } finally {

            try {
                reader.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    @SuppressWarnings("Duplicates")
    @Override
    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
        try {
            // TODO 这里换成 MybatisXMLConfigBuilder 而不是 XMLConfigBuilder
            MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(inputStream, environment, properties);
            return build(parser.parse());
        } catch (Exception e) {
            throw ExceptionFactory.wrapException(e);
        } finally {

            try {
                inputStream.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    // TODO 使用自己的逻辑,注入必须组件
    @Override
    public SqlSessionFactory build(Configuration configuration) {
        GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);

        final IdGenerator idGenerator;
        if (null == globalConfig.getIdGenerator()) {
            idGenerator = DefaultIdGenerator.getInstance();
            globalConfig.setIdGenerator(idGenerator);
        } else {
            idGenerator = globalConfig.getIdGenerator();
        }
        IdWorker.setIdentifierGenerator(idGenerator);

        if (globalConfig.isEnableSqlRunner()) {
            new SqlRunnerInjector().inject(configuration);
        }

        SqlSessionFactory sqlSessionFactory = super.build(configuration);

        // 缓存 sqlSessionFactory
        globalConfig.setSqlSessionFactory(sqlSessionFactory);

        return sqlSessionFactory;
    }
}
